package com.mworld.galaxy.bull.web;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonResponse {
	@JsonProperty("server_status")
	public long serverStatus = 200;
	@JsonProperty("error_code")
	public long errorCode = 0;
	@JsonProperty("message")
	public String message;

	@JsonProperty("data")
	public String getData() {
		if (dataStr != null) {
			return dataStr;
		} else {
			ObjectMapper mapper = new ObjectMapper();
			try {
				return mapper.writeValueAsString(this);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
				return "";
			}
		}
	}

	public Object dataObj;
	public String dataStr;

	public JsonResponse(String message, Object data) {
		this(200, 0, message, data);
	}
	public JsonResponse(long serverStatus, long errorCode, String message, Object data) {
		this.serverStatus = serverStatus;
		this.errorCode = errorCode;
		this.message = message;
		this.dataObj = data;
	}

	public JsonResponse(String message, String data) {
		this(200, 0, message, data);
	}
	public JsonResponse(long serverStatus, long errorCode, String message, String data) {
		this.serverStatus = serverStatus;
		this.errorCode = errorCode;
		this.message = message;
		this.dataStr = data;
	}

	public String toJson(Class<?> view) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			if (view == null) {
				return mapper.writeValueAsString(this);
			} else {
				return mapper.writerWithView(view).writeValueAsString(this);
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}

	static class View {
		public static class BasicView {
		}
		public static class CompleteEntityView {
		}
	}
}
